package com.example.filecollectionsystemdemo1.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.filecollectionsystemdemo1.common.Result;
import com.example.filecollectionsystemdemo1.entity.Classes;
import com.example.filecollectionsystemdemo1.exception.ServiceException;
import com.example.filecollectionsystemdemo1.service.ClassesService;
import com.example.filecollectionsystemdemo1.service.UsersService;
import com.example.filecollectionsystemdemo1.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Classes)表控制层
 *
 * @author makejava
 * @since 2024-10-08 16:58:30
 */
@CrossOrigin
@RestController
@RequestMapping("classes")
public class ClassesController {

    @Autowired
    ClassesService classesService;

    @Autowired
    UsersService usersService;


    /**
     * 查询全部
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Classes> classesList = classesService.list(new QueryWrapper<Classes>().orderByDesc("classesId"));
        return Result.success(classesList);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody Classes classes) {
        try {
            classesService.save(classes);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody Classes classes) {
        classesService.updateById(classes);
        return Result.success();
    }

    /**
     * 删除
     */

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        classesService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除用户信息
     */

    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {  //  [7, 8]
        classesService.removeBatchByIds(ids);
        return Result.success();
    }

    /**
     * 根据ID查询信息
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@RequestParam Integer classesId) {
        Classes classes = classesService.getById(classesId);
        return Result.success(classes);
    }

    /**
     * 多条件模糊查询信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String classname
    ) {
        QueryWrapper<Classes> queryWrapper = new QueryWrapper<Classes>().orderByDesc("ClassID");
        queryWrapper.like(classname != null, "ClassName", classname);
        // select * from user where username like '%#{username}%' and name like '%#{name}%'
        Page<Classes> page = classesService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    /**
     * 获取当前登录用户班级
     */
    @GetMapping("/getCurrentClass")
    public Result getCurrentClass() {
        Integer classid = usersService.getCurrentUser().getClassid();
        Classes classes = classesService.getById(classid);
        return Result.success(classes);
    }


}


